﻿var scene = null;
var renderer = null;
var camera = null;
var camControls = null;
var userheigh = 240;
var box = null;
var bigbox = null;
var levelbox = null;
var boxes = new THREE.Object3D();
var fixible = new THREE.Object3D();
var light = null;

var availableground = null;

// mouse choose item
var mouse = new THREE.Vector2(), hovereditem, ray;

var gui;
var controls = {
	"store": '橡胶仓库',
	"keyword": '',
	"query": function()
	{
		var keyword = controls["keyword"];
		if (keyword == "")
			return;
		if (selectedbox)
			selectedbox.material.color.setHex(0x2a5caa);
		selectedbox = null;
		gui.__controllers[gui.__controllers.length - 1].remove();
		controls["result"] = "";
		$.ajax({
			url:"../index.php/virtual/boxlocation?queryitem=" + keyword,
			type:"post",
			data:"",
			async:false,
			error:function()
			{
				gui.add(controls, "result", []).name("查询错误");
			},
			success:function(data)
			{
				var normalization = data.replace("[", "").replace("]", "");
				while(normalization.indexOf('"') >= 0)
					normalization = normalization.replace('"', '')
				var list = normalization.split(",");
				if (list.length > 0)
					controls["result"] = list[0];
				var guiresult = gui.add(controls, "result", list);
				guiresult.name("查询结果");
				guiresult.onChange(switchCamera);
				switchCamera();
			}
		});
	},
	"result": ''
};

function initUI()
{
	gui = new dat.GUI();
    // 显示仓库和查询框
    var guistore = gui.add(controls, "store", ['橡胶仓库']);
	guistore.onChange(changeStore);
	guistore.name("选择仓库");
    gui.add(controls, "keyword", '').name("输入关键字");
    gui.add(controls, "query").name("------> 查询货架");
	var guiresult = gui.add(controls, "result", []);
	guiresult.name("查询结果");
	guiresult.onChange(switchCamera);
}

function changeStore() {
    if (this.store == "橡胶仓库")
        return;
    location.href = "modelstore.html";
}

var selectedbox = null;

function switchCamera()
{
	var boxname = controls["result"];
	if (boxname == null || boxname == "")
		return;
	var cabinetname = boxname.split("-")[0];
	var intname = parseInt(cabinetname);
	for(var i = 0, l = fixible.children.length; i < l; i++)
	{
		var current = fixible.children[i];
		if (current.name == cabinetname)
		{
			camera.position.copy(current.position);
			camera.position.y = userheigh;
			if (intname <= 42)
			{
				camera.position.x = -2000;
				if (intname <= 22)
				{
					camControls.lon = 180;
					camControls.lat = 120;
				}
				else
				{
					camControls.lon = 0;
					camControls.lat = 120;
				}
			}
			else if (intname >= 60)
			{
				camera.position.set(1500, userheigh, -5000);
			}
			else if (intname >= 48)
			{
				camera.position.x = 1600;
				if (intname <= 56)
				{
					camControls.lon = 180;
					camControls.lat = 120;
				}
				else
				{
					camControls.lon = 0;
					camControls.lat = 120;
				}
			}
			else
			{
				camera.position.z = -600;
				camControls.lon = 90;
				camControls.lat = 120;
			}
			break;
		}
	}
	
	var found = false;
	for(var i = 0, l = boxes.children.length; i < l; i++)
	{
		var current = boxes.children[i];
		if (current.name == boxname)
		{
			found = true;
			if (selectedbox != current)
			{
				if (selectedbox)
					selectedbox.material.color.setHex(0x2a5caa);
				selectedbox = current;
				selectedbox.material.color.setHex(0x00ff00);
			}
		}
	}
	if (found == false)
	{
		alert("格子：" + boxname + " 未找到。")
	}
}

function FilterCabinetBox()
{
	// var boxnamesfromdatabase = [];
	// for(int i = 0; i < boxes.length; ++i)
	// {
	// 	if (boxnamesfromdatabase.contains(boxes[i].name))
	// 		continue;
	// 	boxes[i].visible = false;
	// }
}

function onDocumentMouseMove(event) {
    event.preventDefault();
    mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
    mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}

function onDocumentMouseClick(event) {
	if (!hovereditem)
		return;
    if (!hovereditem.visible)
        return;
    event.preventDefault();
    window.open("../index.php/virtual/boxdetail?store=rubberstore&location=" + hovereditem.name);
}

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
}

function init() {
	initUI();
	
    // 人物移动需要的时钟
    var clock = new THREE.Clock();

    // 初始化场景
    scene = new THREE.Scene();
    CreateBox();

    var ambientLight = new THREE.AmbientLight(0x7f7f7f);
    scene.add(ambientLight);

    initScene();

    ray = new THREE.Raycaster();

    // 初始化webgl
    renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('scene'), antialias: true });
    renderer.setClearColor(new THREE.Color(0xEEEEEE));
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.sortObjects = false;
//    renderer.shadowMapEnabled = true;

    // 初始化人物视角
    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.x = -2000;
    camera.position.y = userheigh;
    camera.position.z = -500;

    camControls = new THREE.FirstPersonControls(camera);
    camControls.lookSpeed = 0.1;
    camControls.movementSpeed = 900;
    camControls.noFly = true;
    camControls.lookVertical = false;
    camControls.constrainVertical = true;
    camControls.verticalMin = 1.59;
    camControls.verticalMax = 1.6;
    camControls.lon = -90;
    camControls.lat = 120;
    camControls.ground = availableground;
	
	addLight(-1400, -4800);
	addLight(-1400, -2700);
	addLight(-1400, -1400);
	addLight(-2600, -4800);
	addLight(-2600, -3500);
	addLight(-2600, -2700);
	addLight(-2600, -1400);
	addLight(-2600, -100);
	addLight(1400, -4800);
	addLight(1400, -2700);
	addLight(1400, -1400);
	addLight(2600, -4800);
	addLight(2600, -3500);
	addLight(2600, -2700);
	addLight(2600, -1400);
	addLight(2600, -100);
	
	addLight(0, -200);
	addLight(-1400, 200);
	
    light = new THREE.SpotLight(0xffffff);
    light.position.copy(camera.position);
    light.position.y = 6000;
    light.castShadow = true;
    light.target = camera;
    scene.add(light);

    document.addEventListener('mousemove', onDocumentMouseMove, false);
    document.addEventListener('click', onDocumentMouseClick, false);
    window.addEventListener('resize', onWindowResize, false);

    var render = function () {
        camControls.update(clock.getDelta());
        renderScene();
        requestAnimationFrame(render);
    };
    render();
}

function addLight(x, z)
{
	var fixedsphere = createMesh2(new THREE.SphereGeometry(1, 1, 1), 0xffffff);
	fixedsphere.position.set(x, 0, z)
    var fixedlight = new THREE.SpotLight(0xffffff);
    fixedlight.position.set(x, 1000, z);
    fixedlight.castShadow = true;
    fixedlight.target = fixedsphere;
    fixible.add(fixedsphere);
	fixible.add(fixedlight);
}

function CreateBox()
{
    box = new THREE.Geometry();
    var color = 0x74787c;

    var vertices = [25,15,20, 25,15,-20, -25,15,-20, -25,15,20,
                    15,-15,10, 15,-15,-10, -15,-15,-10, -15,-15,10];
    var faces = [0,1,2, 0,2,3, 4,5,6, 4,6,7,
                 0,4,5, 0,5,1, 2,6,7, 2,7,3,
                 0,3,7, 0,7,4, 1,2,6, 1,6,5];
    var body = createMesh(new THREE.PolyhedronGeometry(vertices, faces, 30), color);
    body.updateMatrix();
    box.merge(body.geometry, body.matrix);

    var gaizi = createMesh(new THREE.BoxGeometry(50, 5, 40), color);
    gaizi.position.set(0, 15, 0);
    gaizi.updateMatrix();
    box.merge(gaizi.geometry, gaizi.matrix);
	
    bigbox = new THREE.Geometry();

    var bigvertices = [25,10,40, 25,10,-40, -25,10,-40, -25,10,40,
                       10,-10,20, 10,-10,-20, -10,-10,-20, -10,-10,20];
    var bigfaces = [0,1,2, 0,2,3, 4,5,6, 4,6,7,
                    0,4,5, 0,5,1, 2,6,7, 2,7,3,
                    0,3,7, 0,7,4, 1,2,6, 1,6,5];
    var bigbody = createMesh(new THREE.PolyhedronGeometry(bigvertices, bigfaces, 45), color);
    bigbody.updateMatrix();
    bigbox.merge(bigbody.geometry, bigbody.matrix);

    var biggaizi = createMesh(new THREE.BoxGeometry(50, 5, 80), color);
    biggaizi.position.set(0, 10, 0);
    biggaizi.updateMatrix();
    bigbox.merge(biggaizi.geometry, biggaizi.matrix);
	
    levelbox = new THREE.Geometry();

    var levelvertices = [150,10,40, 150,10,-40, -150,10,-40, -150,10,40,
                       100,-10,20, 100,-10,-20, -100,-10,-20, -100,-10,20];
    var levelfaces = [0,1,2, 0,2,3, 4,5,6, 4,6,7,
                    0,4,5, 0,5,1, 2,6,7, 2,7,3,
                    0,3,7, 0,7,4, 1,2,6, 1,6,5];
    var levelbody = createMesh(new THREE.PolyhedronGeometry(levelvertices, levelfaces, 150), color);
    levelbody.updateMatrix();
    levelbox.merge(levelbody.geometry, levelbody.matrix);

    var levelgaizi = createMesh(new THREE.BoxGeometry(310, 5, 80), color);
    levelgaizi.position.set(0, 10, 0);
    levelgaizi.updateMatrix();
    levelbox.merge(levelgaizi.geometry, levelgaizi.matrix);
}

function initScene()
{
    AddStore(600);

    AddCabinet(-2400, 700, 1, "right", [[5,4,3,2,1],[5,4,3,2,1],[5,4,3,2,1],[5,4,3,2,1],[3,2,1]], null);
    AddCabinet(-2800, 700, 1, "right", [[10,9,8,7,6],[10,9,8,7,6],[10,9,8,7,6],[10,9,8,7,6],[6,5,4]], null);

    AddCabinet(-2400, 400, 2, "right", [[19,20,21,22,23,24],[19,20,21,22,23,24],[13,14,15,16,17,18],[10,11,12],[]], [[6,5,4,3,2,1],[6,5,4,3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, 400, 2, "right", [[13,14,15,16,17,18],[13,14,15,16,17,18],[7,8,9,10,11,12],[7,8,9],[]], [[12,11,10,9,8,7],[12,11,10,9,8,7],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, 100, 3, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, 100, 3, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -200, 4, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -200, 4, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -500, 5, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -500, 5, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -800, 6, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -800, 6, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -1100, 7, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -1100, 7, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -1400, 8, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -1400, 8, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -1700, 9, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -1700, 9, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -2000, 10, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -2000, 10, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -2300, 11, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -2300, 11, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -2600, 12, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -2600, 12, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -2900, 13, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -2900, 13, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -3200, 14, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -3200, 14, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -3500, 15, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -3500, 15, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -3800, 16, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -3800, 16, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -4100, 17, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -4100, 17, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -4400, 18, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -4400, 18, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -4700, 19, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -4700, 19, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -5000, 20, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -5000, 20, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -5300, 21, "right", [[10,11,12],[10,11,12],[10,11,12],[10,11,12],[]], [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[3,2,1]]);
    AddCabinet(-2800, -5300, 21, "right", [[7,8,9],[7,8,9],[7,8,9],[7,8,9],[]], [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[6,5,4]]);

    AddCabinet(-2400, -5600, 22, "right", [[3,2,1],[3,2,1],[3,2,1],[3,2,1],[]], null);
    AddCabinet(-2800, -5600, 22, "right", [[6,5,4],[6,5,4],[6,5,4],[6,5,4],[]], null);

    AddCabinet(-1600, -5600, 23, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[]], null);
    AddCabinet(-1200, -5600, 23, "left", [[4,5,6],[4,5,6],[4,5,6],[4,5,6],[]], null);

    AddCabinet(-1600, -5300, 24, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]], null);
    AddCabinet(-1200, -5300, 24, "left", [[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6]], null);

    AddCabinet(-1600, -5200, 25, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[]], null);
    AddCabinet(-1200, -5200, 25, "left", [[4,5,6],[4,5,6],[4,5,6],[4,5,6],[]], null);

    AddCabinet(-1600, -4900, 26, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]], null);
    AddCabinet(-1200, -4900, 26, "left", [[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6]], null);

    AddCabinet(-1600, -4800, 27, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[]], null);
    AddCabinet(-1200, -4800, 27, "left", [[4,5,6],[4,5,6],[4,5,6],[4,5,6],[]], null);

    AddCabinet(-1600, -4500, 28, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]], null);
    AddCabinet(-1200, -4500, 28, "left", [[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6]], null);

    AddCabinet(-1600, -4400, 29, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[]], null);
    AddCabinet(-1200, -4400, 29, "left", [[4,5,6],[4,5,6],[4,5,6],[4,5,6],[]], null);

    AddCabinet(-1600, -4100, 30, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]], null);
    AddCabinet(-1200, -4100, 30, "left", [[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6]], null);

    AddCabinet(-1600, -4000, 31, "left", [[1],[1],[1],[1],[1]], null);
    AddCabinet(-1200, -4000, 31, "left", [[2],[2],[2],[2],[2]], null);
	
    AddCabinet(-1600, -3200, 32, "left", [[1],[1],[1],[1],[1]], null);
    AddCabinet(-1200, -3200, 32, "left", [[2],[2],[2],[2],[2]], null);
	
    AddCabinet(-1600, -3100, 33, "left", [[1],[1],[1],[1],[1]], null);
    AddCabinet(-1200, -3100, 33, "left", [[2],[2],[2],[2],[2]], null);
	
    AddCabinet(-1600, -2800, 34, "left", [[1],[1],[1],[1],[1]], null);
    AddCabinet(-1200, -2800, 34, "left", [[2],[2],[2],[2],[2]], null);
	
    AddCabinet(-1600, -2700, 35, "left", [[1],[1],[1],[1],[1]], null);
    AddCabinet(-1200, -2700, 35, "left", [[2],[2],[2],[2],[2]], null);
	
    AddCabinet(-1600, -2400, 36, "left", [[1],[1],[1],[1],[1]], null);
    AddCabinet(-1200, -2400, 36, "left", [[2],[2],[2],[2],[2]], null);
	
    AddCabinet(-1600, -2300, 37, "left", [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3]], null);
    AddCabinet(-1200, -2300, 37, "left", [[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[4,5,6]], null);
	
    AddCabinet(-1600, -2000, 38, "left", [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3]], null);
    AddCabinet(-1200, -2000, 38, "left", [[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[4,5,6]], null);
	
    AddCabinet(-1600, -1900, 39, "left", [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3]], null);
    AddCabinet(-1200, -1900, 39, "left", [[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[4,5,6]], null);
	
    AddCabinet(-1600, -1600, 40, "left", [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3]], null);
    AddCabinet(-1200, -1600, 40, "left", [[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[4,5,6]], null);
	
    AddCabinet(-1600, -1500, 41, "left", [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3]], null);
    AddCabinet(-1200, -1500, 41, "left", [[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[4,5,6]], null);
	
    AddCabinet(-1600, -1200, 42, "left", [[20,19,18,17,16],[20,19,18,17,16],[20,19,18,17,16],[20,19,18,17,16],[]], [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3]]);
    AddCabinet(-1200, -1200, 42, "left", [[15,14,13,12,11],[15,14,13,12,11],[15,14,13,12,11],[15,14,13,12,11],[]], [[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[4,5,6]]);

    AddCabinet(-800, -2, 43, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3]], null);
    AddCabinet(-380, -2, 44, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3]], null);
    AddCabinet(40, -2, 45, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3]], null);
    AddCabinet(460, -2, 46, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3]], null);
    AddCabinet(880, -2, 47, "left", [[1,2,3],[1,2,3],[1,2,3],[1,2,3]], null);

    AddCabinet(1200, -1200, 48, "right", [[3,2,1],[5,4,3,2,1],[3,2,1],[3,2,1]], null);
    AddCabinet(1200, -1500, 49, "right", [[3,2,1],[5,4,3,2,1],[5,4,3,2,1],[3,2,1]], null);
    AddCabinet(1200, -1800, 50, "right", [[3,2,1],[5,4,3,2,1],[5,4,3,2,1],[3,2,1]], null);
    AddCabinet(1200, -2100, 51, "right", [[3,2,1],[5,4,3,2,1],[5,4,3,2,1],[3,2,1]], null);
    AddCabinet(1200, -2400, 52, "right", [[3,2,1],[5,4,3,2,1],[5,4,3,2,1],[3,2,1]], null);
    AddCabinet(1200, -2700, 53, "right", [[1],[1],[1],[1]], null);
    AddCabinet(1200, -3000, 54, "right", [[3,2,1],[1],[1],[1]], null);
    AddCabinet(1200, -3300, 55, "right", [[3,2,1],[1],[1],[1]], null);
    AddCabinet(1200, -3600, 56, "right", [[3,2,1],[1],[1],[1]], null);

    AddCabinet(2400, -3300, 57, "left", [[1],[1],[1],[1]], null);
    AddCabinet(2800, -3300, 57, "left", [[2],[2],[2],[2]], null);

    AddCabinet(2400, -3600, 58, "left", [[1],[1],[1],[1]], null);
    AddCabinet(2800, -3600, 58, "left", [[2],[2],[2],[2]], null);

    AddCabinet(2400, -3900, 59, "left", [[1],[1],[1],[1]], null);
    AddCabinet(2800, -3900, 59, "left", [[2],[2],[2],[2]], null);

    AddCabinet(2400, -4100, 60, "left", [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]], null);
    AddCabinet(2800, -4100, 60, "left", [[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10],[6,7,8,9,10]], null);

    AddCabinet(2800, -4400, 61, "left", [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]], null);
    AddCabinet(2800, -4700, 62, "left", [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]], null);
    AddCabinet(2800, -5000, 63, "left", [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]], null);
    AddCabinet(2800, -5300, 64, "left", [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]], null);

    AddCabinet(2200, -5600, 65, "right", [[5,4,3,2,1],[5,4,3,2,1],[5,4,3,2,1],[5,4,3,2,1]], null);
    AddCabinet(1700, -5600, 66, "right", [[5,4,3,2,1],[5,4,3,2,1],[5,4,3,2,1],[5,4,3,2,1]], null);
    AddCabinet(1200, -5600, 67, "right", [[5,4,3,2,1],[5,4,3,2,1],[5,4,3,2,1],[5,4,3,2,1]], null);

	FilterCabinetBox();
    scene.add(fixible);
    scene.add(boxes);
}

function AddVerticalCabinet(x, z, code, title, frontids, backids) {
    var cabinet = createCabinet();
    cabinet.rotation.y = Math.PI / 2;
    cabinet.position.set(x, 0, z);
	cabinet.name = code;
    cabinet.castShadow = true;
    fixible.add(cabinet);
	
    AddBoxesForVerticalCabinet(x, z, code, frontids, backids);

    if (title != "left" && title != "right")
        return;
    var content = code.toString();
    if (content.length == 1)
        content = "0" + content;
    var text = createMesh2(new THREE.TextGeometry(content, { size: 45, height: 4 }), 0x5c7a29);
    if (title == "left") {
        text.position.set(x - 88, 212, z + 198);
        text.rotation.y = 0;
    }
    if (title == "right") {
        text.position.set(x - 12, 212, z - 198);
        text.rotation.y = Math.PI;
    }
    text.updateMatrix();
    fixible.add(text);
}

// ids is given from left to right for each layer. from up layer to down layer
function AddCabinet(x, z, code, title, frontids, backids) {
    var cabinet = createCabinet();
    cabinet.position.set(x, 0, z);
	cabinet.name = code;
    cabinet.castShadow = true;
    fixible.add(cabinet);

	if (code == 42 || (code >= 2 && code <= 21))
		AddBoxesForCabinet(x, z, code, frontids, backids);
	else
		AddBixBoxesForCabinet(x, z, code, frontids);

    if (title != "left" && title != "right")
        return;
    var content = code.toString();
    if (content.length == 1)
        content = "0" + content;
    var text = createMesh2(new THREE.TextGeometry(content, { size: 45, height: 4 }), 0x5c7a29);
    if (title == "right")
    {
        text.position.set(x + 198, 212, z - 12);
        text.rotation.y = Math.PI / 2;
    }
    if (title == "left")
    {
        text.position.set(x - 198, 212, z - 88);
        text.rotation.y = Math.PI * 3 / 2;
    }
    text.updateMatrix();
    fixible.add(text);
}

function AddBoxesForVerticalCabinet(x, z, code, frontids, backids) {
    AddBox(3.1415926 / 2, x - 25, 10, z - 30, code + "-1-" + frontids[4][3]);
    AddBox(3.1415926 / 2, x - 25, 10, z - 30 - 60, code + "-1-" + frontids[4][4]);
    AddBox(3.1415926 / 2, x - 25, 10, z - 30 - 120, code + "-1-" + frontids[4][5]);
    AddBox(3.1415926 / 2, x - 25, 10, z + 30, code + "-1-" + frontids[4][2]);
    AddBox(3.1415926 / 2, x - 25, 10, z + 30 + 60, code + "-1-" + frontids[4][1]);
    AddBox(3.1415926 / 2, x - 25, 10, z + 30 + 120, code + "-1-" + frontids[4][0]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 10, z - 30, code + "-1-" + backids[4][3]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 10, z - 30 - 60, code + "-1-" + backids[4][4]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 10, z - 30 - 120, code + "-1-" + backids[4][5]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 10, z + 30, code + "-1-" + backids[4][2]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 10, z + 30 + 60, code + "-1-" + backids[4][1]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 10, z + 30 + 120, code + "-1-" + backids[4][0]);

    AddBox(3.1415926 / 2, x - 25, 60, z - 30, code + "-2-" + frontids[3][3]);
    AddBox(3.1415926 / 2, x - 25, 60, z - 30 - 60, code + "-2-" + frontids[3][4]);
    AddBox(3.1415926 / 2, x - 25, 60, z - 30 - 120, code + "-2-" + frontids[3][5]);
    AddBox(3.1415926 / 2, x - 25, 60, z + 30, code + "-2-" + frontids[3][2]);
    AddBox(3.1415926 / 2, x - 25, 60, z + 30 + 60, code + "-2-" + frontids[3][1]);
    AddBox(3.1415926 / 2, x - 25, 60, z + 30 + 120, code + "-2-" + frontids[3][0]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 60, z - 30, code + "-2-" + backids[3][3]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 60, z - 30 - 60, code + "-2-" + backids[3][4]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 60, z - 30 - 120, code + "-2-" + backids[3][5]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 60, z + 30, code + "-2-" + backids[3][2]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 60, z + 30 + 60, code + "-2-" + backids[3][1]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 60, z + 30 + 120, code + "-2-" + backids[3][0]);

    AddBox(3.1415926 / 2, x - 25, 110, z - 30, code + "-3-" + frontids[2][3]);
    AddBox(3.1415926 / 2, x - 25, 110, z - 30 - 60, code + "-3-" + frontids[2][4]);
    AddBox(3.1415926 / 2, x - 25, 110, z - 30 - 120, code + "-3-" + frontids[2][5]);
    AddBox(3.1415926 / 2, x - 25, 110, z + 30, code + "-3-" + frontids[2][2]);
    AddBox(3.1415926 / 2, x - 25, 110, z + 30 + 60, code + "-3-" + frontids[2][1]);
    AddBox(3.1415926 / 2, x - 25, 110, z + 30 + 120, code + "-3-" + frontids[2][0]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 110, z - 30, code + "-3-" + backids[2][3]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 110, z - 30 - 60, code + "-3-" + backids[2][4]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 110, z - 30 - 120, code + "-3-" + backids[2][5]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 110, z + 30, code + "-3-" + backids[2][2]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 110, z + 30 + 60, code + "-3-" + backids[2][1]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 110, z + 30 + 120, code + "-3-" + backids[2][0]);

    AddBox(3.1415926 / 2, x - 25, 160, z - 30, code + "-4-" + frontids[1][3]);
    AddBox(3.1415926 / 2, x - 25, 160, z - 30 - 60, code + "-4-" + frontids[1][4]);
    AddBox(3.1415926 / 2, x - 25, 160, z - 30 - 120, code + "-4-" + frontids[1][5]);
    AddBox(3.1415926 / 2, x - 25, 160, z + 30, code + "-4-" + frontids[1][2]);
    AddBox(3.1415926 / 2, x - 25, 160, z + 30 + 60, code + "-4-" + frontids[1][1]);
    AddBox(3.1415926 / 2, x - 25, 160, z + 30 + 120, code + "-4-" + frontids[1][0]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 160, z - 30, code + "-4-" + backids[1][3]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 160, z - 30 - 60, code + "-4-" + backids[1][4]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 160, z - 30 - 120, code + "-4-" + backids[1][5]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 160, z + 30, code + "-4-" + backids[1][2]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 160, z + 30 + 60, code + "-4-" + backids[1][1]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 160, z + 30 + 120, code + "-4-" + backids[1][0]);

    AddBox(3.1415926 / 2, x - 25, 210, z - 30, code + "-5-" + frontids[0][3]);
    AddBox(3.1415926 / 2, x - 25, 210, z - 30 - 60, code + "-5-" + frontids[0][4]);
    AddBox(3.1415926 / 2, x - 25, 210, z - 30 - 120, code + "-5-" + frontids[0][5]);
    AddBox(3.1415926 / 2, x - 25, 210, z + 30, code + "-5-" + frontids[0][2]);
    AddBox(3.1415926 / 2, x - 25, 210, z + 30 + 60, code + "-5-" + frontids[0][1]);
    AddBox(3.1415926 / 2, x - 25, 210, z + 30 + 120, code + "-5-" + frontids[0][0]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 210, z - 30, code + "-5-" + backids[0][3]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 210, z - 30 - 60, code + "-5-" + backids[0][4]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 210, z - 30 - 120, code + "-5-" + backids[0][5]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 210, z + 30, code + "-5-" + backids[0][2]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 210, z + 30 + 60, code + "-5-" + backids[0][1]);
    AddBox(3.1415926 / 2 + 3.1415926, x - 75, 210, z + 30 + 120, code + "-5-" + backids[0][0]);
}

function AddBoxesForCabinet(x, z, code, frontids, backids) {
	
	if (frontids != null)
	{
		var totallevel = frontids.length;
		for(var level = 1; level <= totallevel; level++)
		{
			var totalcount = frontids[totallevel - level].length;
			if (totalcount > 0)
			{
				var countdistance = 400 / totalcount;
				for(var count = 1; count <= totalcount; count++)
				{
					var currentx = x - 200 + countdistance / 2 + countdistance * (count - 1);
					AddBox(0, currentx, 10 + 50 * (level - 1), z - 10, code + "-" + level + "-" + frontids[totallevel - level][count - 1]);
				}
			}
		}
	}
	
	if (backids != null)
	{
		var totallevel = backids.length;
		for(var level = 1; level <= totallevel; level++)
		{
			var totalcount = backids[totallevel - level].length;
			if (totalcount > 0)
			{
				var countdistance = 400 / totalcount;
				for(var count = 1; count <= totalcount; count++)
				{
					var currentx = x - 200 + countdistance / 2 + countdistance * (count - 1);
					AddBox(Math.PI, currentx, 10 + 50 * (level - 1), z - 60, code + "-" + level + "-" + backids[totallevel - level][count - 1]);
				}
			}
		}
	}
}

function AddBixBoxesForCabinet(x, z, code, ids)
{
	if (ids == null)
		return;
	var totallevel = ids.length;
	for(var level = 1; level <= totallevel; level++)
	{
		var totalcount = ids[totallevel - level].length;
		if (totalcount > 0)
		{
			if (totalcount == 1)
			{
				AddLevelBox(x, 10 + 50 * (level - 1), z - 10, code + "-" + level + "-" + ids[totallevel - level][0]);
			}
			else
			{
				var countdistance = 400 / totalcount;
				for(var count = 1; count <= totalcount; count++)
				{
					var currentx = x - 200 + countdistance / 2 + countdistance * (count - 1);
					AddBigBox(currentx, 10 + 50 * (level - 1), z - 10, code + "-" + level + "-" + ids[totallevel - level][count - 1]);
				}
			}
		}
	}
}

function AddBox(rotate, x, y, z, code) {

    var object = createMesh(box, 0x2a5caa);
    object.position.set(x, y + 20, z - 15);
    object.rotation.y = rotate;
    object.name = code;
    boxes.add(object);
}

function AddBigBox(x, y, z, code) {

    var object = createMesh(bigbox, 0x2a5caa);
    object.position.set(x, y + 20, z - 40);
    object.name = code;
    boxes.add(object);
}

function AddLevelBox(x, y, z, code) {

    var object = createMesh(levelbox, 0x2a5caa);
    object.position.set(x, y + 20, z - 40);
    object.name = code;
    boxes.add(object);
}

function renderScene() {
    light.position.copy(camera.position);
    light.position.y = 1000;
    camera.position.y = userheigh;
    camera.updateMatrixWorld();

    ray.setFromCamera(mouse, camera);
    var intersects = ray.intersectObjects(boxes.children);
    if (intersects.length > 0) {
        if (hovereditem != intersects[0].object) {
            if (hovereditem)
                hovereditem.material.emissive.setHex(hovereditem.currentHex);
            hovereditem = intersects[0].object;
            hovereditem.currentHex = hovereditem.material.emissive.getHex();
            hovereditem.material.emissive.setHex(0xff0000);
        }
    }
    else {
        if (hovereditem)
            hovereditem.material.emissive.setHex(hovereditem.currentHex);
        hovereditem = null;
    }
    renderer.render(scene, camera);
}

function AddStore(height) {

    var wallcolor = 0x96582a;

    var left = createMesh2(new THREE.BoxGeometry(1, height, 7400), wallcolor);
    left.position.set(-3000, height / 2, -2000);

    var right = createMesh2(new THREE.BoxGeometry(1, height, 5700), wallcolor);
    right.position.set(3000, height / 2, -2850);

    var middle1 = createMesh2(new THREE.BoxGeometry(1, height, 4900), wallcolor);
    middle1.position.set(-1000, height / 2, -3250);

    var middle2 = createMesh2(new THREE.BoxGeometry(1, height, 4900), wallcolor);
    middle2.position.set(1000, height / 2, -3250);

    var middle3 = createMesh2(new THREE.BoxGeometry(1, height, 1700), wallcolor);
    middle3.position.set(-1000, height / 2, 850);

    var front1 = createMesh2(new THREE.BoxGeometry(6000, height, 1), wallcolor);
    front1.position.set(0, height / 2, -5700);

    var front2 = createMesh2(new THREE.BoxGeometry(2000, height, 1), wallcolor);
    front2.position.set(0, height / 2, -800);

    var back1 = createMesh2(new THREE.BoxGeometry(4000, height, 1), wallcolor);
    back1.position.set(1000, height / 2, 0);

    var back2 = createMesh2(new THREE.BoxGeometry(2000, height, 1), wallcolor);
    back2.position.set(-2000, height / 2, 1700);

    var groundcolor = 0xf2eada;
    var ground = createMesh2(new THREE.BoxGeometry(6000, 1, 7400), groundcolor);
    ground.position.set(0, 0, -2000);
    ground.receiveShadow = true;
    fixible.add(ground);

    // office
    var office1 = createMesh2(new THREE.TextGeometry("OFFICE", { size: 80, height: 2 }), 0x411445);
    office1.position.set(-1800, height / 2, 800);
    office1.rotation.y = Math.PI * 3 / 2;
    fixible.add(office1);
    var office11 = createMesh2(new THREE.BoxGeometry(800, height, 1), wallcolor);
    office11.position.set(-1400, height / 2, 700);
    var office12 = createMesh2(new THREE.BoxGeometry(1, height, 1000), wallcolor);
    office12.position.set(-1800, height / 2, 1200);
    var office2 = createMesh2(new THREE.TextGeometry("OFFICE", { size: 80, height: 2 }), 0x411445);
    office2.position.set(-2200, height / 2, 1200);
    office2.rotation.y = Math.PI / 2;
    fixible.add(office2);
    var office21 = createMesh2(new THREE.BoxGeometry(800, height, 1), wallcolor);
    office21.position.set(-2600, height / 2, 700);
    var office22 = createMesh2(new THREE.BoxGeometry(1, height, 1000), wallcolor);
    office22.position.set(-2200, height / 2, 1200);

    // guan qiang
    var ka1 = createMesh2(new THREE.BoxGeometry(800, height, 100), wallcolor);
    ka1.position.set(2600, height / 2, -4050);
    fixible.add(ka1);
    var ka2 = createMesh2(new THREE.BoxGeometry(800, height, 100), wallcolor);
    ka2.position.set(1400, height / 2, -4050);
    fixible.add(ka2);

    // sheng jiang ji
    var lifter = createMesh2(new THREE.BoxGeometry(400, 2, 400), 0x8B0000);
    lifter.position.set(-2000, 0, 1500);
    fixible.add(lifter);
    var liftertext = createMesh2(new THREE.TextGeometry("LIFTER", { size: 80, height: 2 }), 0x411445);
    liftertext.position.set(-2150, 0, 1450);
    liftertext.rotation.x = Math.PI * 3 / 2;
    fixible.add(liftertext);

    // caozuo tai
    var operation = createMesh2(new THREE.BoxGeometry(800, 2, 700), 0x8B0000);
    operation.position.set(-1400, 0, 350);
    fixible.add(operation);
    var operation1 = createMesh2(new THREE.TextGeometry("OPERATION", { size: 80, height: 2 }), 0x411445);
    operation1.position.set(-1700, 0, 300);
    operation1.rotation.x = Math.PI * 3 / 2;
    fixible.add(operation1);
    var operation2 = createMesh2(new THREE.TextGeometry("PLATFORM", { size: 80, height: 2 }), 0x411445);
    operation2.position.set(-1650, 0, 500);
    operation2.rotation.x = Math.PI * 3 / 2;
    fixible.add(operation2);

    // fang kong kou
    var airdefence = createMesh2(new THREE.BoxGeometry(700, 2, 1000), 0x8B0000);
    airdefence.position.set(2400, 0, -2800);
    fixible.add(airdefence);
    var airedefence1 = createMesh2(new THREE.TextGeometry("AIRDEFENCE", { size: 70, height: 2 }), 0x411445);
    airedefence1.position.set(2100, 0, -2800);
    airedefence1.rotation.x = Math.PI * 3 / 2;
    fixible.add(airedefence1);
    var airedefence2 = createMesh2(new THREE.TextGeometry("HOLE", { size: 80, height: 2 }), 0x411445);
    airedefence2.position.set(2250, 0, -2600);
    airedefence2.rotation.x = Math.PI * 3 / 2;
    fixible.add(airedefence2);

    // daichuwuzi
    var pending1 = createMesh2(new THREE.TextGeometry("PENDING", { size: 80, height: 2 }), 0x411445);
    pending1.position.set(2150, 0, -1500);
    pending1.rotation.x = Math.PI * 3 / 2;
    fixible.add(pending1);
    var pending2 = createMesh2(new THREE.TextGeometry("METERIALS", { size: 70, height: 2 }), 0x411445);
    pending2.position.set(2100, 0, -1200);
    pending2.rotation.x = Math.PI * 3 / 2;
    fixible.add(pending2);

    // fa huo qu
    var shipping = createMesh2(new THREE.BoxGeometry(800, 2, 400), 0x8B0000);
    shipping.position.set(1400, 0, -4300);
    fixible.add(shipping);
    var shipping1 = createMesh2(new THREE.TextGeometry("SHIPPING", { size: 80, height: 2 }), 0x411445);
    shipping1.position.set(1200, 0, -4300);
    shipping1.rotation.x = Math.PI * 3 / 2;
    fixible.add(shipping1);

    // zhuzi
    var column = createMesh2(new THREE.BoxGeometry(300, 2, 300), 0x8B0000);
    column.position.set(1150, 0, -5150);
    fixible.add(column);


    // safe way
    var leftsafe = createMesh2(new THREE.BoxGeometry(400, 2, 7000), 0xfcaf17);
    leftsafe.position.set(-2000, 0, -2200);
    fixible.add(leftsafe);
    var centersafe = createMesh2(new THREE.BoxGeometry(4800, 2, 700), 0xfcaf17);
    centersafe.position.set(600, 0, -450);
    fixible.add(centersafe);
    var rightsafe = createMesh2(new THREE.BoxGeometry(400, 2, 3250), 0xfcaf17);
    rightsafe.position.set(1600, 0, -2425);
    fixible.add(rightsafe);
    var safe1 = createMesh2(new THREE.BoxGeometry(800, 2, 600), 0xfcaf17);
    safe1.position.set(-1400, 0, -3650);
    fixible.add(safe1);
    var safe2 = createMesh2(new THREE.BoxGeometry(800, 2, 400), 0xfcaf17);
    safe2.position.set(-1400, 0, -200);
    fixible.add(safe2);
    var safe3 = createMesh2(new THREE.BoxGeometry(400, 2, 1900), 0xfcaf17);
    safe3.position.set(2000, 0, -4350);
    fixible.add(safe3);
    var safe4 = createMesh2(new THREE.BoxGeometry(800, 2, 500), 0xfcaf17);
    safe4.position.set(1400, 0, -4750);
    fixible.add(safe4);
    var safe5 = createMesh2(new THREE.BoxGeometry(400, 2, 1100), 0xfcaf17);
    safe5.position.set(2400, 0, -4750);
    fixible.add(safe5);

    fixible.add(left);
    fixible.add(right);
    fixible.add(middle1);
    fixible.add(middle2);
    fixible.add(middle3);
    fixible.add(front1);
    fixible.add(front2);
    fixible.add(back1);
    fixible.add(back2);
    fixible.add(office11);
    fixible.add(office12);
    fixible.add(office21);
    fixible.add(office22);

    availableground = [{ x: 2998, y: -2 }, { x: 2998, y: -3998 }, { x: 2198, y: -3998 },
              { x: 2198, y: -4102 }, { x: 2998, y: -4102 },
              { x: 2998, y: -5698 }, { x: 1002, y: -5698 },
              { x: 1002, y: -4102 }, { x: 1802, y: -4102 }, { x: 1802, y: -3998 },
              { x: 1002, y: -3998 },
              { x: 1002, y: -798 }, { x: -1002, y: -798 }, { x: -1002, y: -5698 },
              { x: -2998, y: -5698 }, { x: -2998, y: 698 }, { x: -2198, y: 698 },
              { x: -2198, y: 1298 }, { x: -1802, y: 1298 }, { x: -1802, y: 698 },
              { x: -1002, y: 698 }, { x: -1002, y: -2 }, ];
}

function createCabinet() {
    var x = 50 * 6 + 5 * 10 + 25 * 2; // 6个箱子，间隔10，两边有25
    var y = 30 * 5 + 20 * 5 + 10; // 上下5排，间隔20
    var z = 40 * 2 + 10 + 5 * 2; // 前后2排，间隔10，两边5

    var geometry = new THREE.Geometry();
    var color = 0x74787c;

    var left1 = createMesh2(new THREE.BoxGeometry(4, 4, z + 4), color);
    var left3 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    var left4 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    left1.position.set(-x / 2, y, -z / 2);
    left3.position.set(-x / 2, y / 2, -z);
    left4.position.set(-x / 2, y / 2, 0);
    var right1 = createMesh2(new THREE.BoxGeometry(4, 4, z + 4), color);
    var right3 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    var right4 = createMesh2(new THREE.BoxGeometry(4, y, 4), color);
    right1.position.set(x / 2, y, -z / 2);
    right3.position.set(x / 2, y / 2, -z);
    right4.position.set(x / 2, y / 2, 0);

    left1.updateMatrix();
    left3.updateMatrix();
    left4.updateMatrix();
    right1.updateMatrix();
    right3.updateMatrix();
    right4.updateMatrix();
    geometry.merge(left1.geometry, left1.matrix);
    geometry.merge(left3.geometry, left3.matrix);
    geometry.merge(left4.geometry, left4.matrix);
    geometry.merge(right1.geometry, right1.matrix);
    geometry.merge(right3.geometry, right3.matrix);
    geometry.merge(right4.geometry, right4.matrix);

    var bottom = createMesh2(new THREE.BoxGeometry(x, 8, z), color);
    var h1 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h2 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h3 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);
    var h4 = createMesh2(new THREE.BoxGeometry(x, 4, z), color);

    bottom.position.set(0, 10, -z / 2);
    h1.position.set(0, 60, -z / 2);
    h2.position.set(0, 110, -z / 2);
    h3.position.set(0, 160, -z / 2);
    h4.position.set(0, 210, -z / 2);

    bottom.updateMatrix();
    geometry.merge(bottom.geometry, bottom.matrix);
    h1.updateMatrix();
    geometry.merge(h1.geometry, h1.matrix);
    h2.updateMatrix();
    geometry.merge(h2.geometry, h2.matrix);
    h3.updateMatrix();
    geometry.merge(h3.geometry, h3.matrix);
    h4.updateMatrix();
    geometry.merge(h4.geometry, h4.matrix);

    return new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: color }));
}

function createMesh(geometry, colorvalue) {
    var meterial = new THREE.MeshPhongMaterial({ color: colorvalue });
    meterial.side = THREE.DoubleSide;
    return new THREE.Mesh(geometry, meterial);
}

function createMesh2(geometry, colorvalue)
{
    var meterial = new THREE.MeshPhongMaterial({ color: colorvalue });
    meterial.side = THREE.DoubleSide;
    return new THREE.Mesh(geometry, meterial);
}
